home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / ps.c < prev    next >
C/C++ Source or Header  |  1993-04-23  |  45KB  |  1,379 lines

  1. /*
  2.  * ps.c -- Postscript scanning and copying routines.
  3.  * Copyright (C) 1992  Timothy O. Theisen
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *   Author: Tim Theisen           Systems Programmer
  20.  * Internet: tim@cs.wisc.edu       Department of Computer Sciences
  21.  *     UUCP: uwvax!tim             University of Wisconsin-Madison
  22.  *    Phone: (608)262-0438         1210 West Dayton Street
  23.  *      FAX: (608)262-9777         Madison, WI   53706
  24.  *
  25.  * This file has been modified by Russell Lang (rjl@monu1.cc.monash.edu.au)
  26.  * 1993-04-23
  27.  * - Header file includes changed for MS-Windows.
  28.  * - MS-DOS carriage return handling added.
  29.  * - Ignore ^D as first character of file
  30.  * - Allow %%Pages: 0
  31.  */
  32.  
  33. #include <stdio.h>
  34. #ifdef __TURBOC__
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #define strcasecmp(s,t) stricmp(s,t)
  39. extern void pserror(char *str);
  40. #else
  41. #ifndef SEEK_SET
  42. #define SEEK_SET 0
  43. #endif
  44. #ifndef BUFSIZ
  45. #define BUFSIZ 1024
  46. #endif
  47. #include <ctype.h>
  48. #include <X11/Xos.h>        /* #includes the appropriate <string.h> */
  49. #define pserror(str) fprintf(stderr,str)
  50. #endif
  51. #include "ps.h"
  52.  
  53. #ifdef BSD4_2
  54. #define memset(a,b,c) bzero(a,c)
  55. #endif
  56.  
  57. /* length calculates string length at compile time */
  58. /* can only be used with character constants */
  59. #define length(a) (sizeof(a)-1)
  60. #define iscomment(a, b)    (strncmp(a, b, length(b)) == 0)
  61.  
  62.     /* list of standard paper sizes from Adobe's PPD. */
  63.  
  64. struct documentmedia papersizes[] = {
  65.     "Letter",         612,  792,
  66.     "LetterSmall",     612,  792,
  67.     "Tabloid",         792, 1224,
  68.     "Ledger",        1224,  792,
  69.     "Legal",         612, 1008,
  70.     "Statement",     396,  612,
  71.     "Executive",     540,  720,
  72.     "A3",         842, 1190,
  73.     "A4",         595,  842,
  74.     "A4Small",         595,  842,
  75.     "A5",         420,  595,
  76.     "B4",         729, 1032,
  77.     "B5",         516,  729,
  78.     "Folio",         612,  936,
  79.     "Quarto",         610,  780,
  80.     "10x14",         720, 1008,
  81.     NULL,           0,    0
  82. };
  83.  
  84.  
  85. static char *readline();
  86. static char *gettextline();
  87. static char *gettext();
  88. static int  blank();
  89.  
  90. /*
  91.  *    psscan -- scan the PostScript file for document structuring comments.
  92.  *
  93.  *    This scanner is designed to retrieve the information necessary for
  94.  *    the ghostview previewer.  It will scan files that conform to any
  95.  *    version (1.0, 2.0, 2.1, or 3.0) of the document structuring conventions.
  96.  *    It does not really care which version of comments the file contains.
  97.  *    (The comments are largely upward compatible.)  It will scan a number
  98.  *    of non-conforming documents.  (You could have part of the document
  99.  *    conform to V2.0 and the rest conform to V3.0.  It would be similar
  100.  *    to the DC-2 1/2+, it would look funny but it can still fly.)
  101.  *
  102.  *    This routine returns a pointer to the document structure.
  103.  *    The structure contains the information relevant to previewing.
  104.  *      These include EPSF flag (to tell if the file is a encapsulated figure),
  105.  *      Page Media (for the Page Size), Bounding Box (to minimize backing
  106.  *      pixmap size or determine window size for encapsulated PostScript), 
  107.  *      Orientation of Paper (for default transformation matrix), and
  108.  *      Page Order.  The title and CreationDate are also retrieved to
  109.  *      help identify the document.
  110.  *
  111.  *      The following comments are examined:
  112.  *
  113.  *      Header section: 
  114.  *      Must start with %!PS-Adobe-.  Version numbers ignored.
  115.  *
  116.  *      %!PS-Adobe-* [EPSF-*]
  117.  *      %%BoundingBox: <int> <int> <int> <int>|(atend)
  118.  *      %%CreationDate: <textline>
  119.  *      %%Orientation: Portrait|Landscape|(atend)
  120.  *      %%Pages: <uint> [<int>]|(atend)
  121.  *      %%PageOrder: Ascend|Descend|Special|(atend)
  122.  *      %%Title: <textline>
  123.  *      %%DocumentMedia: <text> <real> <real> <real> <text> <text>
  124.  *      %%DocumentPaperSizes: <text>
  125.  *      %%EndComments
  126.  *
  127.  *      Note: Either the 3.0 or 2.0 syntax for %%Pages is accepted.
  128.  *            Also either the 2.0 %%DocumentPaperSizes or the 3.0
  129.  *            %%DocumentMedia comments are accepted as well.
  130.  *
  131.  *      The header section ends either explicitly with %%EndComments or
  132.  *      implicitly with any line that does not begin with %X where X is
  133.  *      a not whitespace character.
  134.  *
  135.  *      If the file is encapsulated PostScript the optional Preview section
  136.  *      is next:
  137.  *
  138.  *      %%BeginPreview
  139.  *      %%EndPreview
  140.  *
  141.  *      This section explicitly begins and ends with the above comments.
  142.  *
  143.  *      Next the Defaults section for version 3 page defaults:
  144.  *
  145.  *      %%BeginDefaults
  146.  *      %%PageBoundingBox: <int> <int> <int> <int>
  147.  *      %%PageOrientation: Portrait|Landscape
  148.  *      %%PageMedia: <text>
  149.  *      %%EndDefaults
  150.  *
  151.  *      This section explicitly begins and ends with the above comments.
  152.  *
  153.  *      The prolog section either explicitly starts with %%BeginProlog or
  154.  *      implicitly with any nonblank line.
  155.  *
  156.  *      %%BeginProlog
  157.  *      %%EndProlog
  158.  *
  159.  *      The Prolog should end with %%EndProlog, however the proglog implicitly
  160.  *      ends when %%BeginSetup, %%Page, %%Trailer or %%EOF are encountered.
  161.  *
  162.  *      The Setup section is where the version 2 page defaults are found.
  163.  *      This section either explicitly begins with %%BeginSetup or implicitly
  164.  *      with any nonblank line after the Prolog.
  165.  *
  166.  *      %%BeginSetup
  167.  *      %%PageBoundingBox: <int> <int> <int> <int>
  168.  *      %%PageOrientation: Portrait|Landscape
  169.  *      %%PaperSize: <text>
  170.  *      %%EndSetup
  171.  *
  172.  *      The Setup should end with %%EndSetup, however the setup implicitly
  173.  *      ends when %%Page, %%Trailer or %%EOF are encountered.
  174.  *
  175.  *      Next each page starts explicitly with %%Page and ends implicitly with
  176.  *      %%Page or %%Trailer or %%EOF.  The following comments are recognized:
  177.  *
  178.  *      %%Page: <text> <uint>
  179.  *      %%PageBoundingBox: <int> <int> <int> <int>|(atend)
  180.  *      %%PageOrientation: Portrait|Landscape
  181.  *      %%PageMedia: <text>
  182.  *      %%PaperSize: <text>
  183.  *
  184.  *      The tralier section start explicitly with %%Trailer and end with %%EOF.
  185.  *      The following comment are examined with the proper (atend) notation
  186.  *      was used in the header:
  187.  *
  188.  *      %%Trailer
  189.  *      %%BoundingBox: <int> <int> <int> <int>|(atend)
  190.  *      %%Orientation: Portrait|Landscape|(atend)
  191.  *      %%Pages: <uint> [<int>]|(atend)
  192.  *      %%PageOrder: Ascend|Descend|Special|(atend)
  193.  *      %%EOF
  194.  *
  195.  *
  196.  *  + A DC-3 received severe damage to one of its wings.  The wing was a total
  197.  *    loss.  There was no replacement readily available, so the mechanic
  198.  *    installed a wing from a DC-2.
  199.  */
  200.  
  201. struct document *
  202. psscan(file)
  203.     FILE *file;
  204. {
  205.     struct document *doc;
  206.     int bb_set = NONE;
  207.     int pages_set = NONE;
  208.     int page_order_set = NONE;
  209.     int orientation_set = NONE;
  210.     int page_bb_set = NONE;
  211.     int page_media_set = NONE;
  212.     int preread;        /* flag which tells the readline isn't needed */
  213.     int i;
  214.     unsigned int maxpages = 0;
  215.     unsigned int nextpage = 1;    /* Next expected page */
  216.     unsigned int thispage;
  217.     int ignore = 0;        /* whether to ignore page ordinals */
  218.     char *label;
  219.     char line[PSLINELENGTH];    /* 255 characters + 1 newline + 1 NULL */
  220.     char text[PSLINELENGTH];    /* Temporary storage for text */
  221.     long position;        /* Position of the current line */
  222.     unsigned int line_len;     /* Length of the current line */
  223.     unsigned int section_len;    /* Place to accumulate the section length */
  224.     char *next_char;